package com.zto.tbschedule.console.controller;

import com.zto.tbschedule.console.service.IScheduleService;
import com.zto.tbschedule.console.tbschedule.ScheduleServer;
import com.zto.tbschedule.console.tbschedule.ScheduleStrategy;
import com.zto.tbschedule.console.tbschedule.ScheduleTaskItem;
import com.zto.tbschedule.console.tbschedule.ScheduleTaskType;
import com.zto.tbschedule.console.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/task")
public class ScheduleController {
    @Autowired
    private IScheduleService scheduleService;

    @RequestMapping("/tasklist.do")
    @ResponseBody
    public Message<List<ScheduleTaskType>> listTask(HttpServletRequest request,Long appId){
        /*User user = LoginHelp.getUser(request);
        if(!LoginHelp.isAdmin(user)){
            return new Message<List<ScheduleTaskType>>(false,Message.ILLEGAL_OPERATION,"无权操作",null);
        }*/
        try{
            List<ScheduleTaskType> taskList = scheduleService.getScheduleTaskTypeListByAppId(appId);
            return new Message<List<ScheduleTaskType>>(true,Message.SUCCESS,taskList);
        }catch (Exception e){
            e.printStackTrace();
            return new Message<List<ScheduleTaskType>>(false,Message.SYSTEM_ERROR,e.getMessage(),null);
        }
    }
    @RequestMapping("/taskDetailList.do")
    @ResponseBody
    public Message<TaskDetail> listTaskDetail(HttpServletRequest request,Long appId,String taskName){
        /*User user = LoginHelp.getUser(request);
        if(!LoginHelp.isAdmin(user)){
            return new Message<List<ScheduleTaskType>>(false,Message.ILLEGAL_OPERATION,"无权操作",null);
        }*/
        TaskDetail taskDetail = new TaskDetail();
        try{
            List<ScheduleStrategy> scheduleStrategyList = scheduleService.getScheduleStrategyListByTaskNameAndAppId(appId, taskName);
            List<ScheduleServer> scheduleServerList = scheduleService.getScheduleServerListByTaskNameAndAppId(appId, taskName);
            List<ScheduleTaskItem> scheduleTaskItemList = scheduleService.getScheduleTaskItemListByTaskNameAndAppId(appId,taskName);
            taskDetail.setStrategyList(scheduleStrategyList);
            taskDetail.setScheduleServerList(scheduleServerList);
            taskDetail.setTaskItemList(scheduleTaskItemList);
            return new Message<TaskDetail>(true,Message.SUCCESS,taskDetail);
        }catch (Exception e){
            e.printStackTrace();
            return new Message<TaskDetail>(false,Message.SYSTEM_ERROR,e.getMessage(),null);
        }
    }
    @RequestMapping("/editTask.do")
    @ResponseBody
    public Message<Boolean> editTask(HttpServletRequest request,Task task){
        try{
            scheduleService.editTask(task);
            return new Message<Boolean>(true,"操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new Message<Boolean>(false,Message.SYSTEM_ERROR,e.getMessage(),false);
        }
    }
    @RequestMapping("/clearTask.do")
    @ResponseBody
    public Message<Boolean> clearTask(HttpServletRequest request,Task task){
        try{
            scheduleService.clearTask(task);
            return new Message<Boolean>(true,"操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new Message<Boolean>(false,Message.SYSTEM_ERROR,e.getMessage(),false);
        }
    }

    @RequestMapping("/delTask.do")
    @ResponseBody
    public Message<Boolean> delTask(HttpServletRequest request,Task task){
        try{
            scheduleService.delTask(task);
            return new Message<Boolean>(true,"操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new Message<Boolean>(false,Message.SYSTEM_ERROR,e.getMessage(),false);
        }
    }
    @RequestMapping("/backupTask.do")
    @ResponseBody
    public Message<Boolean> backupTask(HttpServletRequest request,Long appId){
        try{
            scheduleService.backupTask(appId);
            return new Message<Boolean>(true,"操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new Message<Boolean>(false,Message.SYSTEM_ERROR,e.getMessage(),false);
        }
    }

    @RequestMapping("/delBackupTask.do")
    @ResponseBody
    public Message<Boolean> delBackupTask(HttpServletRequest request,Long id){
        try{
            scheduleService.delBackupTask(id);
            return new Message<Boolean>(true,"操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new Message<Boolean>(false,Message.SYSTEM_ERROR,e.getMessage(),false);
        }
    }

    @RequestMapping("/recoveryBackupTask.do")
    @ResponseBody
    public Message<Boolean> recoveryBackupTask(HttpServletRequest request,Long id){
        try{
            scheduleService.recoveryBackupTask(id);
            return new Message<Boolean>(true,"操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new Message<Boolean>(false,Message.SYSTEM_ERROR,e.getMessage(),false);
        }
    }

    @RequestMapping("/backupTaskList.do")
    @ResponseBody
    public Message<List<BackTask>> backupTaskList(HttpServletRequest request,Long appId){
        try{
            return new Message<List<BackTask>>(true,"操作成功",scheduleService.searchBackupTask(appId));
        }catch (Exception e){
            e.printStackTrace();
            return new Message<List<BackTask>>(false,Message.SYSTEM_ERROR,e.getMessage(),null);
        }
    }




    @RequestMapping("/editTaskSchedule.do")
    @ResponseBody
    public Message<Boolean> editTaskSchedule(HttpServletRequest request,TaskSchedule taskSchedule){
        try{
            scheduleService.editTaskSchedule(taskSchedule);
            return new Message<Boolean>(true,"操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new Message<Boolean>(false,Message.SYSTEM_ERROR,e.getMessage(),false);
        }
    }
    @RequestMapping("/editTaskScheduleSts.do")
    @ResponseBody
    public Message<Boolean> editTaskScheduleSts(HttpServletRequest request,TaskSchedule taskSchedule){
        try{
            scheduleService.editTaskScheduleSts(taskSchedule);
            return new Message<Boolean>(true,"操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new Message<Boolean>(false,Message.SYSTEM_ERROR,e.getMessage(),false);
        }
    }
    @RequestMapping("/delTaskSchedule.do")
    @ResponseBody
    public Message<Boolean> delTaskSchedule(HttpServletRequest request,TaskSchedule taskSchedule){
        try{
            scheduleService.delTaskSchedule(taskSchedule);
            return new Message<Boolean>(true,"操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new Message<Boolean>(false,Message.SYSTEM_ERROR,e.getMessage(),false);
        }
    }


}
